package b4.view.component.smithy
{
	import b4.config.BasicObjConfig;
	import b4.config.Config;
	import b4.config.EquipConfig;
	import b4.model.vo.player.BasicObj;
	import b4.model.vo.player.Equip;
	import b4.model.vo.player.EquipProperty;
	import b4.view.component.basic.TitledPanel;
	import b4.view.component.events.ItemClickEvent;
	import flash.events.MouseEvent;
	
	import org.aswing.ASColor;
	import org.aswing.BorderLayout;
	import org.aswing.JButton;
	import org.aswing.JPanel;
	import org.aswing.JTextField;
	import org.aswing.SoftBoxLayout;
	import org.aswing.border.LineBorder;
	
	public class EquipRefine extends BaseSmithy
	{
		public var equipBox:BoxObj;
		public var stockBox:BoxObj;
		public var refineButton:JButton;
		public var payPane:TitledPanel;
		public var payTxt:JTextField;
		private var leftTopContentRight:JPanel;
		private static const stoneColorList:Object = {1:10009, 2: 10010,3:10011,4: 10012};//1:"绿",2 "蓝", 3"黄", 4"紫";
		public function EquipRefine()
		{
			super("请放入要洗练的装备", "洗练说明");
			
			tryToLoadRs = {1:"请先放入装备", 2:"请放入对应颜色的洗练石"};
			equipBox = new BoxObj("装备", 1, 1028);
			stockBox = new BoxObj("洗练石", 10009, 10012);	
			equipBox.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			stockBox.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);
			boxArr.push(equipBox, stockBox);

			payPane = new TitledPanel("费用（铜币）", 200, 55);
			payTxt = new JTextField();
			payTxt.setPreferredHeight(20);
			payTxt.setPreferredWidth(75);
			payPane.append(payTxt);
			//500, 150
			var leftTopContentLeft:JPanel = new JPanel();
			leftTopContentLeft.setPreferredHeight(90);
			leftTopContentLeft.setPreferredWidth(200);
			leftTopContentRight = new JPanel(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			leftTopContentRight.setPreferredHeight(60);
			leftTopContentRight.setPreferredWidth(300);
			leftTopContentRight.setBorder(new LineBorder(null, ASColor.BLUE));
			leftTopContent.append(leftTopContentLeft, BorderLayout.WEST);
			leftTopContent.append(leftTopContentRight, BorderLayout.EAST);
			leftTopContent.append(payPane, BorderLayout.SOUTH);
			leftTopContentLeft.appendAll(equipBox, stockBox)

			refineButton = new JButton("开始洗练");
			leftBottom.append(refineButton);
		}
		
		override public function tryToLoadItem(obj:BasicObj):int
		{
			var basicObjConfig:BasicObjConfig = Config.objConfigs[obj.index];		
			var objType:int = basicObjConfig.type;
			if(objType != BasicObjConfig.TYPE_EQUIP && !equipBox.hasObj)
				return 1;
			if(objType != BasicObjConfig.TYPE_EQUIP)
			{
				var equip:Equip = equipBox.boxItem.obj as Equip;
				if (obj.index != stoneColorList[equip.quality])
					return 2;
			}
			return 0;
		}
		
		override public function loadItem(obj:BasicObj):BoxObj
		{
			var returnBox:BoxObj;
			returnBox = super.loadItem(obj);
			if (returnBox != null && obj is Equip)
			{
				var equip:Equip = obj as Equip;
				leftTopContentRight.removeAll();
				for (var i:int = 0; i < equip.equipPropertyList.length; i++)
				{
					var properTxt:JTextField = new JTextField();
					var equipProperty:EquipProperty = equip.equipPropertyList[i];
					properTxt.setText(EquipConfig.EQUIP_PROPERTY_NAMES[equipProperty.propertyIndex]+"+"+equipProperty.propertyNum+ 
					"    洗炼" + equipProperty.level + "星");
					leftTopContentRight.append(properTxt);
				}
			}
			refreshPayTxt();
			return returnBox;
		}
		
		private function onLoadItemOff(e:ItemClickEvent):void
		{
			var boxObj:BoxObj = e.currentTarget as BoxObj;				
			if(boxObj is Equip)
				stockBox.loadOffItem();
			var tabLoadOffEvent:ItemClickEvent = new ItemClickEvent(ItemClickEvent.TAB_LOAD_ITEM_OFF)
			dispatchEvent(tabLoadOffEvent);
			refreshPayTxt();
		}
		
		private function refreshPayTxt():void
		{	 
			var payNum:int = 0;
			if (equipBox.hasObj && (equipBox.boxItem.obj is Equip))
			{
				var equipItem:Equip = equipBox.boxItem.obj as Equip;
				payNum = equipItem.quality * 100;
				payTxt.setText(String(payNum));
			}
			else
			{
				payTxt.setText("");
			}
			
		}
	}
}
